<template>
  <div class="home-container">
    <!-- 轮播图 -->
    <el-carousel height="400px" class="banner" indicator-position="outside">
      <el-carousel-item v-for="banner in banners" :key="banner.id">
        <router-link :to="banner.link">
          <img :src="banner.image" :alt="banner.title" class="banner-img">
          <div class="banner-title">{{ banner.title }}</div>
        </router-link>
      </el-carousel-item>
    </el-carousel>

    <!-- 药品展示 -->
    <div class="content">
      <div class="section-header">
        <h3>药品信息展示</h3>
        <router-link to="/user/drugs" class="more-link">查看更多 ></router-link>
      </div>

      <!-- 分类标签 -->
      <div class="category-tabs">
        <el-tabs v-model="activeCategory" @tab-click="handleCategoryChange">
          <el-tab-pane label="全部" name="all"></el-tab-pane>
          <el-tab-pane v-for="category in categories" :key="category.id" :label="category.name" :name="category.name">
          </el-tab-pane>
        </el-tabs>
      </div>

      <!-- 搜索栏 -->
      <div class="search-bar">
        <el-input
          v-model="searchQuery"
          placeholder="请输入药品名称"
          class="search-input"
          clearable
        >
          <template #prefix>
            <el-icon><Search /></el-icon>
          </template>
        </el-input>
        <el-button type="primary" @click="handleSearch">搜索</el-button>
      </div>

      <!-- 药品列表 -->
      <div class="drug-list">
        <el-row :gutter="20">
          <el-col :span="6" v-for="drug in displayDrugs" :key="drug.id">
            <el-card :body-style="{ padding: '0px' }" class="drug-card">
              <img :src="drug.image" class="drug-image">
              <div class="drug-info">
                <h4>{{ drug.name }}</h4>
                <p class="drug-spec">规格：{{ drug.specification }}</p>
                <p class="drug-manufacturer">厂家：{{ drug.manufacturer }}</p>
                <div class="drug-price">
                  <span class="price">¥{{ drug.price }}</span>
                  <el-button type="primary" size="small" @click="addToCart(drug)">加入购物车</el-button>
                </div>
              </div>
            </el-card>
          </el-col>
        </el-row>
      </div>

      <!-- 分页 -->
      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :total="totalDrugs"
          :page-sizes="[8, 16, 24, 32]"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { Search } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { getDrugs, getCategories, getBanners } from '../api'

const banners = ref([])
const activeCategory = ref('all')
const searchQuery = ref('')
const currentPage = ref(1)
const pageSize = ref(8)
const totalDrugs = ref(0)
const categories = ref([])
const drugs = ref([])
const displayDrugs = ref([])

const loadBanners = async () => {
  try {
    const { data } = await getBanners()
    banners.value = data
  } catch (error) {
    ElMessage.error('获取轮播图失败')
  }
}

const loadCategories = async () => {
  try {
    const { data } = await getCategories()
    categories.value = data
  } catch (error) {
    ElMessage.error('获取分类失败')
  }
}

const loadDrugs = async () => {
  try {
    const { data } = await getDrugs()
    drugs.value = data
    totalDrugs.value = data.length
    updateDisplayDrugs()
  } catch (error) {
    ElMessage.error('获取药品失败')
  }
}

const updateDisplayDrugs = () => {
  let filteredDrugs = drugs.value

  // 分类筛选
  if (activeCategory.value !== 'all') {
    filteredDrugs = filteredDrugs.filter(drug => drug.category === activeCategory.value)
  }

  // 搜索筛选
  if (searchQuery.value) {
    filteredDrugs = filteredDrugs.filter(drug => 
      drug.name.toLowerCase().includes(searchQuery.value.toLowerCase()) ||
      drug.manufacturer.toLowerCase().includes(searchQuery.value.toLowerCase())
    )
  }

  // 分页
  const start = (currentPage.value - 1) * pageSize.value
  const end = start + pageSize.value
  displayDrugs.value = filteredDrugs.slice(start, end)
  totalDrugs.value = filteredDrugs.length
}

const handleCategoryChange = () => {
  currentPage.value = 1
  updateDisplayDrugs()
}

const handleSearch = () => {
  currentPage.value = 1
  updateDisplayDrugs()
}

const handleSizeChange = (val) => {
  pageSize.value = val
  updateDisplayDrugs()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  updateDisplayDrugs()
}

const addToCart = async (drug) => {
  try {
    // 这里需要调用添加到购物车的API
    ElMessage.success('添加成功')
  } catch (error) {
    ElMessage.error('添加失败')
  }
}

onMounted(() => {
  loadBanners()
  loadCategories()
  loadDrugs()
})
</script>

<style scoped>
.home-container {
  min-height: 100%;
  padding: 20px;
}

.banner {
  margin-bottom: 20px;
  border-radius: 8px;
  overflow: hidden;
  width: 100%;
  max-width: 1200px;
  margin-left: auto;
  margin-right: auto;
}

.banner-img {
  width: 100%;
  height: 100%;
  object-fit: fill;
  display: block;
}

.banner-title {
  position: absolute;
  bottom: 20px;
  left: 20px;
  color: #fff;
  font-size: 24px;
  font-weight: bold;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.5);
}

.content {
  background: #fff;
  padding: 20px;
  border-radius: 8px;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.section-header h3 {
  margin: 0;
  font-size: 20px;
  color: #333;
}

.more-link {
  color: #409EFF;
  text-decoration: none;
}

.category-tabs {
  margin-bottom: 20px;
}

.search-bar {
  display: flex;
  gap: 10px;
  margin-bottom: 20px;
}

.search-input {
  width: 300px;
}

.drug-list {
  margin-bottom: 20px;
}

.drug-card {
  margin-bottom: 20px;
  transition: all 0.3s;
}

.drug-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.drug-image {
  width: 100%;
  height: 200px;
  object-fit: cover;
}

.drug-info {
  padding: 15px;
}

.drug-info h4 {
  margin: 0 0 10px;
  font-size: 16px;
  color: #333;
}

.drug-spec,
.drug-manufacturer {
  margin: 5px 0;
  color: #666;
  font-size: 14px;
}

.drug-price {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 15px;
}

.price {
  color: #f56c6c;
  font-size: 20px;
  font-weight: bold;
}

.pagination {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}
</style> 